<?php

namespace Admin\Controller;

use Think\Controller;
use Admin\Model\TougaoModel;
use Admin\Model\CategoryModel;
use Home\Model\UserModel;

class TougaoController extends CommonController {
	public function index() {
		$data ['nav'] = 'tougao';
		$do = I ( 'get.do' );
		$id = I ( 'get.id' );
		$page = I ( 'get.p' );
		$article = new TougaoModel ();
		$category = new CategoryModel ();
		$where = 'type="article"';
		$data ['category'] = $category->search ( $where );
		if ($id) {
			$data ['info'] = $article->getInfo ( $id );
		}
		if ($do == 'save') {
			$rs = $article->saveData ( $_POST, $_POST ['id'] );
			if ($rs ['status']) {
				$this->success ( $rs ['info'], '/admin.php?m=Admin&c=Tougao' );
			} else {
				$this->error ( $rs ['info'] );
			}
			exit ();
		} else {
			$where = '1';
			if ($_GET ['status'] != '') {
				$where .= ' and status=' . intval ( $_GET ['status'] );
			}
			if ($_GET ['pay'] == 1) {
				$where .= ' and pay is not null ';
			} else if ($_GET ['pay'] == - 1) {
				$where .= ' and pay is  null ';
			}
			if ($_GET ['title'] != '') {
				$where .= ' and title like "%' . $_GET ['title'] . '%"';
			}

			if ($_GET ['username']) {
				$user = new UserModel ();
				$userInfo = $user->getUserInfoForName ( $_GET ['username'] );
				$where .= ' and uid=' . intval ( $userInfo ['uid'] );
			}

			$data ['article'] = $article->search ( $where, $page, ' updatetime desc',50 );
		}
		$data ['title'] = '投稿管理';
		$data ['do'] = $do;
		$this->data = $data;
		$this->display ( 'tougao' );
	}
	public function setStatus() {
		$tougao = new TougaoModel ();
		$id = intval ( $_REQUEST ['id'] );

		$rs = $tougao->setStatus ( $id, 0 );
		if ($rs) {
			$this->success ( '审核成功', '/admin.php?m=Admin&c=Tougao' );
		} else {
			$this->error ( '审核失败', '/admin.php?m=Admin&c=Tougao' );
		}
	}
	public function saveToArticle() {
		$id = intval ( $_REQUEST ['id'] );
		$tougao = new TougaoModel ();
		$rs = $tougao->saveToArticle ( $id );
		if ($rs ['status']) {
			$this->success ( $rs ['msg'] );
		} else {
			$this->error ( $rs ['msg'] );
		}
	}
	public function setPay() {
		$id = intval ( $_REQUEST ['id'] );
		$tougao = new TougaoModel ();
		$rs = $tougao->setPay ( $id );
		if ($rs) {
			$this->success ( '操作成功', '/admin.php?m=Admin&c=Tougao' );
		} else {
			$this->error ( '操作失败', '/admin.php?m=Admin&c=Tougao' );
		}
	}
	public function autoToArticle() {
		$tougao = new TougaoModel ();
		$rs = $tougao->autoToArticle ();
		if ($rs ['status']) {
			$this->success ( '操作成功' . $rs ['msg'], '/admin.php?m=Admin&c=Tougao' );
		} else {
			$this->error ( '操作失败!' . $rs ['msg'], '/admin.php?m=Admin&c=Tougao' );
		}
	}
}
